<?php

//dezend by http://www.yunlu99.com/

if (!defined('IN_IA')) {

	exit('Access Denied');

}



require_once EWEI_SHOPV2_PLUGIN . 'app/core/page_mobile.php';

class Mylive_EweiShopV2Page extends AppMobilePage

{

	protected $member;



	public function __construct()

	{

		global $_W;

		parent::__construct();

		$this->member = m('member')->getInfo($_W['openid']);

	}



	protected function diyformData()

	{

		$template_flag = 0;

		$diyform_plugin = p('diyform');



		if ($diyform_plugin) {

			$set_config = $diyform_plugin->getSet();

			$user_diyform_open = $set_config['user_diyform_open'];



			if ($user_diyform_open == 1) {

				$template_flag = 1;

				$diyform_id = $set_config['user_diyform'];



				if (!empty($diyform_id)) {

					$formInfo = $diyform_plugin->getDiyformInfo($diyform_id);

					$fields = $formInfo['fields'];

					$diyform_data = iunserializer($this->member['diymemberdata']);

					$f_data = $diyform_plugin->getDiyformData($diyform_data, $fields, $this->member);

				}

			}

		}



		$appDatas = array();



		if ($diyform_plugin) {

			$appDatas = $diyform_plugin->wxApp($fields, $f_data, $this->member);

		}



		return array('template_flag' => $template_flag, 'f_data' => $appDatas['f_data'], 'fields' => $appDatas['fields'], 'set_config' => $set_config, 'diyform_plugin' => $diyform_plugin, 'formInfo' => $formInfo, 'diyform_id' => $diyform_id, 'diyform_data' => $diyform_data);

	}



	public function main()

	{

		global $_GPC;

		global $_W;

		$openid=$_W['openid'];

		$user = pdo_fetch('select id,openid,headimgurl,nickname from '.tablename('wzbagent_users').' where  uniacid=:uniacid and  openid=:openid',array(':openid'=>$_W['openid'],':uniacid'=>$_W['uniacid']));

		

		$live = pdo_fetch('SELECT * FROM ' . tablename('wzbagent_lives') . ' WHERE `uniacid` = :uniacid AND `anchor` = :id', array(':uniacid' => $_W['uniacid'],':id' => $user['id']) );
		if ($live['start_at']){
            $live['start_at']=date('Y-m-d H:i:s',$live['start_at']);
        }else{
        	 $live['start_at']=date('Y-m-d H:i:s',time());
        }
        $rewardData = pdo_fetch('select isshow from '.tablename('wzbagent_rewards').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);
        $live['reward']=$rewardData['isshow']==1 ? 1 : 0;
         //开屏广告
        $spreadsData = pdo_fetch('select * from '.tablename('wzbagent_spreads').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);
        $live['spreads_images']['src']='';
        $live['spreads_isshow']=0;
        if(!empty($spreadsData)){
            $live['spreads_count_time']=$spreadsData['count_time'];
            $live['spreads_images'] =$spreadsData['images'];
          
           // $live['images'] = $spreadsData['images'];
            $live['spreads_isshow']=intval($spreadsData['isshow']);
            $live['spreads_url']=$spreadsData['url'];

        }

        //权限查看
        $limitsData = pdo_fetch('select * from '.tablename('wzbagent_limits').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);
        $live['limits_type']=0;
        if(!empty($limitsData)){
            $live['limits_type']=intval($limitsData['type']);
            $live['limits_password']=$limitsData['password'];
            $live['limits_delayed']=$limitsData['delayed'];
            $live['limits_amount']=$limitsData['amount'];
            
        }
        /*邀请卡*/
        $invitations=pdo_fetch('select * from '.tablename('wzbagent_invitations').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);
        $invitations['invitations_bg']['src']='';
        if(!empty($limitsData)){
        	$live['invitations_bg'] =$invitations['bg'];
        	$live['invitations_desc1'] =$invitations['desc1'];
        	$live['invitations_desc2'] =$invitations['desc2'];
        	$live['invitations_desc3'] =$invitations['desc3'];
        	$live['invitations_desc4'] =$invitations['desc4'];
        }

		$live_isshow[0]=array('id'=>0,'name'=>'关闭');
        $live_isshow[1]=array('id'=>1,'name'=>'开启');

        $video_type = pdo_fetchall('select code as id,name from '.tablename('wzbagent_lookups').' where pid=54 and code in (0,3,4,5,7,11)');
        //$live_type = pdo_fetchall('select code as id,name from '.tablename('wzbagent_lookups').' where pid=51');
        $live_zhuangtai = pdo_fetchall('select code as id,name from '.tablename('wzbagent_lookups').' where pid=10 order by code asc');
        $live_quanxian = pdo_fetchall('select code as id,name from '.tablename('wzbagent_lookups').' where pid=27 and code in (0,1,2,3,4)');

		$live['live_status']=intval($live['live_status']);
		if(!empty($live_zhuangtai)){
			$live_zhuangtaiarray=[];
			foreach ($live_zhuangtai as $key => $zhuangtai) {
				$live_zhuangtaiarray[]=$zhuangtai['name'];
			}
		}
		if(!empty($live_quanxian)){
			$live_quanxianarray=[];
			foreach ($live_quanxian as $key => $quanxian) {
				$live_quanxianarray[]=$quanxian['name'];
			}
		}
		$live_isshowarray=['关闭','开启'];

		return app_json(array('live'=>$live,'live_isshow'=>$live_isshow,'live_zhuangtai'=>$live_zhuangtai,'live_quanxian'=>$live_quanxian,'live_zhuangtaiarray'=>$live_zhuangtaiarray,'live_isshowarray'=>$live_isshowarray,'live_quanxianarray'=>$live_quanxianarray));

	}



	public function submit()

	{

		global $_W;

		global $_GPC;

		

		$openid=$_W['openid'];

		$user = pdo_fetch('select id,openid,headimgurl,nickname from '.tablename('wzbagent_users').' where  uniacid=:uniacid and  openid=:openid',array(':openid'=>$_W['openid'],':uniacid'=>$_W['uniacid']));						

		$live = pdo_fetch('SELECT * FROM ' . tablename('wzbagent_lives') . ' WHERE `uniacid` = :uniacid AND `anchor` = :id', array(':uniacid' => $_W['uniacid'],':id' => $user['id']) );				

		$arr = array('title' => trim($_GPC['title']),'online_num' => intval($_GPC['online_num']),'fans_num' => intval($_GPC['fans_num']), 'intro' => trim($_GPC['intro']));

		$images = $_GPC['images'];
        $images1 = $_GPC['images1'];

		if(is_string($images)){
			$aa=explode(',',$images);
		}

		// var_dump(count($images));

		if (count($images)>=1){			
			$arr['live_cover']=$images[0];
            //图片压缩
            //$old_live_cover=$images1[0];
            //var_dump($old_live_cover);
            //images/18/2020/02/eKK60m9PLLpVv9ap06090c8KnEvMe0.jpg
            //$result = m('imgcompressor')->set($old_live_cover, $old_live_cover)->resize(300,0)->get();
            //$result = m('common')->compressedImage($arr['live_cover'], ATTACHMENT_ROOT.'aa.jpg');
            //var_dump($result);
		}
		$arr['start_at']=strtotime($_GPC['dateTime']);
		$arr['live_status']=intval($_GPC['live_status']);
		$arr['rollcontent']=trim($_GPC['rollcontent']);
		pdo_update('wzbagent_lives', $arr, array('id' => $live['id'], 'uniacid' => $_W['uniacid']));

		$rewardsData = pdo_fetch('select * from '.tablename('wzbagent_rewards').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);

		$data_rewards=array(
            'isshow' => $_GPC['reward']==true ? 1 : 0
        );
        if(empty($rewardsData)){
        	$data_rewards['uniacid']=$_W['uniacid'];
            $data_rewards['rid']=$live['rid'];
            $data_rewards['live_id']=$live['id'];
            $data_rewards['nickname']=$user['nickname'];
            $data_rewards['desc']='赞是美意，赏是鼓励';
            $data_rewards['images']=$user['headimgurl'];
            
            $reward=array(
				'one'=>array('money'=>'1','remark'=>'独一无二'),
				'two'=>array('money'=>'3','remark'=>'垂涎三尺'),
				'three'=>array('money'=>'5','remark'=>'学富五车'),
				'four'=>array('money'=>'666','remark'=>'溜得很'),
				'five'=>array('money'=>'888','remark'=>'恭喜发财'),
				'six'=>array('money'=>'999','remark'=>'一言九鼎'),
			);
			$data_rewards['setting']=json_encode($reward);

        }else{
        	pdo_update('wzbagent_rewards',$data_rewards,array('rid'=>$live['rid'],'uniacid'=>$_W['uniacid']));
   		 }
        //
        $spreadsData = pdo_fetch('select * from '.tablename('wzbagent_spreads').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);
        //var_dump($spreadsData);
        //var_dump($data_spreads);
        //exit();

        $data_spreads['count_time']=$_GPC['spreads_count_time'];

        $spreads_images = $_GPC['spreads_images'];

		if(is_string($spreads_images)){
			$bb=explode(',',$spreads_images);
		}
		if (count($spreads_images)>=1){			
			$data_spreads['images']=$spreads_images[0];
		}
		$data_spreads['isshow']=0;
        //$data_spreads['images']=$_GPC['spreads_images'];
        $data_spreads['isshow']=$_GPC['spread_isshow'];  

              
        $data_limits['type']=$_GPC['limits_type'];
        //$data_limits['type']=0;
        $data_limits['password']=$_GPC['limits_password'];
        $data_limits['delayed']=$_GPC['limits_delayed'];
        $data_limits['amount']=$_GPC['limits_amount'];

        $invitations_bg = $_GPC['invitations_bg'];

		if(is_string($invitations_bg)){
			$bb=explode(',',$invitations_bg);
		}
		if (count($invitations_bg)>=1){			
			$data_invitations['bg']=$invitations_bg[0];
		}

        $data_invitations['desc1']=$_GPC['invitations_desc1'];
        $data_invitations['desc2']=$_GPC['invitations_desc2'];
        $data_invitations['desc3']=$_GPC['invitations_desc3'];
        $data_invitations['desc4']=$_GPC['invitations_desc4'];



        $data_spreads['bgcolor']='#000000';
        $data_spreads['color']='#ff0000';
        $data_spreads['timecolor']='#ff8080';
        if (empty($spreadsData)){
            $data_spreads['uniacid']=$_W['uniacid'];
            $data_spreads['rid']=$live['rid'];
            pdo_insert('wzbagent_spreads',$data_spreads);
            $spreadsData['id']=pdo_insertid();
            //cache_write('wzbagent_uniacid_'.$_W['uniacid'].'_spread_live_rid_'.$live['rid'],$spread);
        }else{
           // var_dump($data_spreads);
           // var_dump($spreadsData['id']);
            pdo_update('wzbagent_spreads',$data_spreads,array('id'=>$spreadsData['id']));

            
        }
        $spread = pdo_fetch('SELECT * FROM ' . tablename('wzbagent_spreads') . ' WHERE `uniacid` = :uniacid and `rid` = :rid and `isshow` = :isshow', array(':uniacid' => $_W['uniacid'],':rid' => $live['rid']));
        //cache_write('wzbagent_uniacid_'.$_W['uniacid'].'_spread_live_rid_'.$live['rid'],$spread);

        $limitsData = pdo_fetch('select * from '.tablename('wzbagent_limits').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);

        if (empty($limitsData)){
            $data_limits['uniacid']=$_W['uniacid'];
            $data_limits['rid']=$live['rid'];
            $data_limits['live_id']=$live['id'];
            pdo_insert('wzbagent_limits',$data_limits);
        }else{
            pdo_update('wzbagent_limits',$data_limits,array('id'=>$limitsData['id']));
        }
	
        /*邀请卡*/
        $invitationsData=pdo_fetch('select * from '.tablename('wzbagent_invitations').' where rid='.$live['rid'].' and uniacid='.$live['uniacid']);
        if (empty($invitationsData)){
            $data_invitations['uniacid']=$_W['uniacid'];
            $data_invitations['rid']=$live['rid'];
            $data_invitations['live_id']=$live['id'];
            pdo_insert('wzbagent_invitations',$data_invitations);
        }else{
            pdo_update('wzbagent_invitations',$data_invitations,array('id'=>$invitationsData['id']));
        }
		



		return app_json(1,'保存成功');

	}



	public function tuiliuurl(){

		global $_GPC;

		global $_W;

		$openid=$_W['openid'];

		$user = pdo_fetch('select id,openid,headimgurl,nickname from '.tablename('wzbagent_users').' where  uniacid=:uniacid and  openid=:openid',array(':openid'=>$_W['openid'],':uniacid'=>$_W['uniacid']));

		$live = pdo_fetch('SELECT * FROM ' . tablename('wzbagent_lives') . ' WHERE `uniacid` = :uniacid AND `anchor` = :id', array(':uniacid' => $_W['uniacid'],':id' => $user['id']) );

		

		require(IA_ROOT . '/framework/library/qrcode/phpqrcode.php');

        load()->func('tpl');

        load()->func('file');

        $id=$live['id'];



        $config = m('appzb')->getconfig();



        $live = pdo_fetch('SELECT id,video_type,rid,uniacid FROM ' . tablename('wzbagent_lives') . ' WHERE `uniacid` = :uniacid AND `id` = :id', array(':uniacid' => $_W['uniacid'],':id' => $id) );

        if ($live['video_type']==0){

            $pushUrl = m('appzb')->PrivateKeyA(time(),$config['PushFlow']['value'],'/'.$live['id'].'_'.$live['rid'].'/'.$live['rid'].'_'.$live['uniacid'],$config['AuthMainKey']['value'],$config['PushFlowExt']['value']);



			

            $imgUrl = "/addons/wxz_wzbagent/attachment/wzbagent_livepush".$live['id']."_".$live['rid'].".png";



            $errorCorrectionLevel = "L";

            $matrixPointSize = "4";

            QRcode::png($pushUrl,IA_ROOT .$imgUrl,$errorCorrectionLevel,$matrixPointSize);

        }



        $live_url = $_W['siteroot'].str_replace('./','app/',$this->createMobileurl('index',array('rid'=>$live['rid'])));

        $live_url = '/pages/live/room/index?live_id'.$live['id'];

		

		

		return app_json(array(

            'live_url'=>$live_url,

            'img_url'=>$imgUrl,

            'live'=>$live,

            'push_url'=>$pushUrl

        ));

	}

	

}



?>

